PATHMac OS 8 and 9 Developer Documentation > Interapplication Communication > AppleScript for Scripters >

AppleScript Language Guide

   

Set

The Set command can function as an AppleScript command or an application command. The AppleScript command assigns one or more values to one or more variables. It can also be used to share data among lists, records, or script objects--see the Notes section below and Data Sharing. The application command sets the values of one or more objects.

APPLESCRIPT COMMAND SYNTAX
set variablePattern to expression expression returning variablePattern
APPLICATION COMMAND SYNTAX
set referencePattern to expression expression returning referencePattern
PARAMETERS
variablePattern
The name of the variable in which to store the value, or a list of variable patterns, or a record of variable patterns. Class: Identifier, list, or record
expression
The expression whose value or values are to be assigned. If expression is a reference or a list or record of references, AppleScript gets the values of the objects specified by the references. Class: For a variable, any class.
referencePattern
A reference to the location whose value is to be set, or a list of reference patterns, or a record of reference patterns. Class: Reference, list, or record
RESULT

The value assigned.

EXAMPLES

You can use the Set command to set a variable to any value:

set x to 5
--result: 5
set myList to { 1, 2, "four" }
--result: {1, 2, "four"}
tell application "AppleWorks"
    set x to word 1 of text body of front document
end tell

These two statements are equivalent:

set x to 3
3 returning x
--result: 3

Similarly, the following examples are equivalent:

tell front document of application "AppleWorks"
    set x to word 1 of text body
end tell
tell front document of application "AppleWorks"
    word 1 of text body returning x
end tell

In addition to setting a variable to a single value, you can set patterns of variables to patterns of values. For example, this script sets a list of two variables to the position of the front window.

tell application "Finder"
    set {x, y} to position of front window
end tell

Since the Finder returns position of front window as a list of two integers, the preceding example sets x to the first item in the list and y to the second item.

Patterns set with the Set command can also be more complex. Here are some examples:

set x to {8, 94133, {firstName:"John", lastName:"Chapman"}}
set {p, q, r} to x
(* now p, q, and r have these values:
                p = 8
                q = 94133
                r = {firstName:"John", lastName:"Chapman"} *)
set {p, q, {lastName:r}} to x
(* now p, q, and r have these values: p = 8
                                      q = 94133
                                      r = "Chapman" *)

As the last example demonstrates, the properties of a record need not be given in the same order and need not all be used when you set a pattern to a pattern, as long as the patterns match.

The use of the Set command with patterns is similar to the use of patterned parameters with subroutines, which is described in Subroutines With Positional Parameters.

NOTES

If you use the Set command to set a variable to a list, record, or script object, the variable shares data with the original list, record, or script object. If you change the data of the original, the value of the variable also changes. Here's an example of how this works:

set myList to { 1, 2, 3 } set yourList to myList set item 1 of myList to 4

The result of these statements is that item 1 of both myList and yourList is 4 .

Data sharing promotes efficiency when using large data structures. Rather than making copies of shared data, the same data can belong to multiple structures. When one structure is updated, the others are automatically updated.

IMPORTANT

To avoid data sharing for lists, records, and script objects, you typically copy these items with the AppleScript command Copy, rather than use the Set command.

Only data in lists, records, and script objects can be shared; you cannot share other values. Moreover, you can share data only on the same computer, and the shared structures must all be in the same script.


© 1999 Apple Computer, Inc. – (Last Updated 21 May 99)